package com.itdianzi.controller.system;

import com.github.pagehelper.PageInfo;
import com.itdianzi.controller.base.BaseController;
import com.itdianzi.domain.system.Module;
import com.itdianzi.service.system.ModuleService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/system/module")
public class ModuleController extends BaseController {
    @Autowired
    private ModuleService moduleService;
    @RequestMapping(value = "/list",name = "模块查询")
    public String list(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "5") Integer size) {
        PageInfo pageInfo = moduleService.findAll(page, size);
        request.setAttribute("page",pageInfo);

        return "/system/module/module-list";
    }
    @RequestMapping(value = "/toAdd",name = "跳转到模块添加页面")
    public String toAdd() {
        List<Module> moduleList = moduleService.findAll();
        request.setAttribute("menus",moduleList);
        return "/system/module/module-add";
    }
    @RequestMapping(value = "/edit",name = "模块添加或修改")
    public String edit(Module module) {
        if (StringUtils.isBlank(module.getId())) {
            moduleService.save(module);
        } else {
            moduleService.update(module);
        }
        return "redirect:/system/module/list.do";
    }
    @RequestMapping(value = "/toUpdate",name = "跳转到模块修改页面")
    public String toUpdate(String id) {
       Module module = moduleService.findById(id);
        request.setAttribute("module", module);

        List<Module> moduleList = moduleService.findAll();
        request.setAttribute("menus",moduleList);
        return "/system/module/module-update";
    }
    @RequestMapping(value = "/delete",name = "模块数据删除")
    public String delete(String id) {
        moduleService.delete(id);
        return "redirect:/system/module/list.do";
    }

}
